<template>
  <div class="editForm-wrapper">
    <el-dialog
      v-if="method === 'add' || 'edit'"
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="visible"
    >

      <el-form label-width="96px" :model="inputForm" :rules="rules" ref="inputForm">
        <el-form-item label="类型名称:" prop="description">
          <el-input  v-model="inputForm.description" placeholder='请输入类型名称'></el-input>
        </el-form-item>

        <!-- 字典类型(0:系统字典 1:业务字典) -->
        <el-form-item label="字典类型:" prop="types">
          <el-select clearable v-model="inputForm.types" placeholder='请选择字典类型'>
            <el-option
              v-for="item in  $dictUtils.getDictList('types')"
              :key="item.value"
              :label="item.label"
              :value="Number(item.value)">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="代码:" prop="type">
          <el-input v-model="inputForm.type" placeholder='请输入代码，数字或者字母'></el-input>
        </el-form-item>

        <el-form-item label="排序号:" prop="sort">
          <el-input-number  :step="1" v-model="inputForm.sort" placeholder="排序号"></el-input-number>
        </el-form-item>
        <!--  //租户权限(tenantAutho) 0不可见 1可见不可编辑 2可编辑  -->
        <el-form-item label="租户权限:" prop="tenantAutho">
          <el-select clearable v-model="inputForm.tenantAutho" placeholder='请选择租户权限'>
            <el-option
              v-for="item in  $dictUtils.getDictList('tenant_autho')"
              :key="item.value"
              :label="item.label"
              :value="Number(item.value)">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <!-- <section class="hint-container">*注意：名称和代码为必填项，保存时需进行校验</section> -->
      <span slot="footer" class="dialog-footer">
        <div class="footer-input">
          <el-button size="small" v-if="method != 'view'" type="primary" @click="doSubmit()" v-noMoreClick>确定</el-button>
          <el-button size="small" @click="visible = false">取消</el-button>
        </div>

      </span>
    </el-dialog>

  </div>

</template>

<script>

export default {
  data () {
    return {
      visible: false,
      loading: false,
      method: '',
      title: '新增',
      inputForm: {
        id: '',
        name: '',
        sort: 1,
        type: '',
        tenantAutho: '',
        description: '',
        types: '',
        code: '',
        remarks: ''
      },
      rules: {
        description: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        sort: [{ required: true, message: '排列序号', trigger: 'blur' }],
        type: [
          { required: true, message: '代码不能为空', trigger: 'change' }
        ]
      }
    }
  },

  methods: {
  	handleClose(done) {
		this.$Msg.confirm({
			title:'您确定要关闭吗？',
			content:this.GLOBAL.FORM_CLOSE_TIPS,
			confirmCall:()=>{
				done();
			}
		})
	},
    init (method, obj) {
      this.inputForm.id = obj
      console.log(obj)
      this.method = method
      if (method === 'add') {
        this.title = '新增字典值'
        this.visible = true
        // 清空表单
        this.$nextTick(() => {
          this.$refs['inputForm'].resetFields()
        })
      } else if (method === 'edit') {
        this.title = '修改字典值'
        this.visible = true
        this.$nextTick(() => {
          if (method === 'edit') { // 修改或者查看
            this.$http({
              url: `/sys/dict/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.dictType)
              this.inputForm.oldLoginName = this.inputForm.loginName
            })
          }
        })
      }
    },
    // 表单提交
    doSubmit () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/sys/dict/save`,
            method: 'post',
            data: this.inputForm
          }).then(({ data }) => {
            this.loading = false
            if (data && data.success) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.visible = false
              this.addChildVisible = false
              this.$emit('refreshDataList')
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .hint-container{
    // // height: 11px;
    // font-size: 12px;
    // // font-weight: 400;
    // color: #FF1F1F;

  }
</style>
